﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;

using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.Support.Device
{
    /// <summary>
    /// 终端
    /// </summary>
    public class Terminal
    {
        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="ip">IP</param>
        /// <param name="values">数据</param>
        /// <returns>发送数据长度</returns>
        public static int Run(string ip, byte[] values)
        {
            int result = 0;

            IPAddress address = IPAddress.None;
            bool validity = IPAddress.TryParse(ip, out address);
            if (validity)
            {
                EndPoint remoteEP = new IPEndPoint(address, Variable.Port);
                try
                {
                    result = Hardware.Socket.SendTo(values, remoteEP);
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log((IPEndPoint)Hardware.Socket.LocalEndPoint, values);
                }
                catch (Exception exception)
                {
                    EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                }
                finally
                {
                }
            }

            return result;
        }
    }
}
